package presentacion.vista;

import accesoDatos.conexion.Conexion;
import accesoDatos.conexion.Peticion;
import accesoDatos.util.Log;
import accesoDatos.vo.Empleado;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import presentacion.util.Ventana;

public class IniciarSesion extends javax.swing.JFrame {

    private static IniciarSesion instancia = null;
    private ArrayList<VistaMesero> meseros = new ArrayList<VistaMesero>();
    private VistaCocina cocina = null;
    private String contraseña = "";

    /** Creates new form IniciarSesion3 */
    private IniciarSesion() {
        initComponents();
        Ventana.pantallaCompleta(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelPrincipal = new javax.swing.JPanel();
        panelIniciarSesion = new javax.swing.JPanel();
        botonTitulo = new javax.swing.JToggleButton();
        labelMensaje = new javax.swing.JLabel();
        campoContraseña = new javax.swing.JTextField();
        botonBorrarContraseña = new javax.swing.JButton();
        boton1 = new javax.swing.JButton();
        boton2 = new javax.swing.JButton();
        boton3 = new javax.swing.JButton();
        boton4 = new javax.swing.JButton();
        boton5 = new javax.swing.JButton();
        boton6 = new javax.swing.JButton();
        boton7 = new javax.swing.JButton();
        boton8 = new javax.swing.JButton();
        boton9 = new javax.swing.JButton();
        separador = new javax.swing.JSeparator();
        panelPedidosEnCurso = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Cliente - Iniciar Sesion");
        setResizable(false);
        setUndecorated(true);

        botonTitulo.setFont(new java.awt.Font("Tahoma", 0, 24));
        botonTitulo.setSelected(true);
        botonTitulo.setText("INICIO DE SESION");
        botonTitulo.setBorder(null);
        botonTitulo.setBorderPainted(false);
        botonTitulo.setFocusPainted(false);
        botonTitulo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonTituloActionPerformed(evt);
            }
        });

        labelMensaje.setFont(new java.awt.Font("Tahoma", 0, 18));
        labelMensaje.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        labelMensaje.setText("Digite su contraseña:");

        campoContraseña.setBackground(java.awt.Color.white);
        campoContraseña.setEditable(false);
        campoContraseña.setFont(new java.awt.Font("Tahoma", 0, 24));
        campoContraseña.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        campoContraseña.setDisabledTextColor(java.awt.Color.black);

        botonBorrarContraseña.setFont(new java.awt.Font("Tahoma", 0, 24));
        botonBorrarContraseña.setText("CE");
        botonBorrarContraseña.setFocusPainted(false);
        botonBorrarContraseña.setMargin(new java.awt.Insets(2, 1, 2, 1));
        botonBorrarContraseña.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBorrarContraseñaActionPerformed(evt);
            }
        });

        boton1.setFont(new java.awt.Font("Tahoma", 0, 36));
        boton1.setText("1");
        boton1.setFocusPainted(false);
        boton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                digitoActionPerformed(evt);
            }
        });

        boton2.setFont(new java.awt.Font("Tahoma", 0, 36));
        boton2.setText("2");
        boton2.setFocusPainted(false);
        boton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                digitoActionPerformed(evt);
            }
        });

        boton3.setFont(new java.awt.Font("Tahoma", 0, 36));
        boton3.setText("3");
        boton3.setFocusPainted(false);
        boton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                digitoActionPerformed(evt);
            }
        });

        boton4.setFont(new java.awt.Font("Tahoma", 0, 36));
        boton4.setText("4");
        boton4.setFocusPainted(false);
        boton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                digitoActionPerformed(evt);
            }
        });

        boton5.setFont(new java.awt.Font("Tahoma", 0, 36));
        boton5.setText("5");
        boton5.setFocusPainted(false);
        boton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                digitoActionPerformed(evt);
            }
        });

        boton6.setFont(new java.awt.Font("Tahoma", 0, 36));
        boton6.setText("6");
        boton6.setFocusPainted(false);
        boton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                digitoActionPerformed(evt);
            }
        });

        boton7.setFont(new java.awt.Font("Tahoma", 0, 36));
        boton7.setText("7");
        boton7.setFocusPainted(false);
        boton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                digitoActionPerformed(evt);
            }
        });

        boton8.setFont(new java.awt.Font("Tahoma", 0, 36));
        boton8.setText("8");
        boton8.setFocusPainted(false);
        boton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                digitoActionPerformed(evt);
            }
        });

        boton9.setFont(new java.awt.Font("Tahoma", 0, 36));
        boton9.setText("9");
        boton9.setFocusPainted(false);
        boton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                digitoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelIniciarSesionLayout = new javax.swing.GroupLayout(panelIniciarSesion);
        panelIniciarSesion.setLayout(panelIniciarSesionLayout);
        panelIniciarSesionLayout.setHorizontalGroup(
            panelIniciarSesionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelIniciarSesionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelIniciarSesionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(botonTitulo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
                    .addGroup(panelIniciarSesionLayout.createSequentialGroup()
                        .addComponent(labelMensaje, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                        .addGap(84, 84, 84))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, panelIniciarSesionLayout.createSequentialGroup()
                        .addGroup(panelIniciarSesionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(panelIniciarSesionLayout.createSequentialGroup()
                                .addGroup(panelIniciarSesionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(boton1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(boton4, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(boton7, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(panelIniciarSesionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(boton5, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                    .addComponent(boton8, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                    .addComponent(boton2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(campoContraseña, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelIniciarSesionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(botonBorrarContraseña, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(boton6, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(boton9, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(boton3, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))))
                .addContainerGap())
        );

        panelIniciarSesionLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {boton1, boton2, boton3, boton4, boton5, boton6, boton7, boton8, boton9, botonBorrarContraseña});

        panelIniciarSesionLayout.setVerticalGroup(
            panelIniciarSesionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelIniciarSesionLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(botonTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                .addComponent(labelMensaje)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelIniciarSesionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(campoContraseña, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonBorrarContraseña, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addGroup(panelIniciarSesionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelIniciarSesionLayout.createSequentialGroup()
                        .addComponent(boton1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelIniciarSesionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(boton4, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(boton5, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(boton6, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelIniciarSesionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(boton7, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(boton8, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(boton9, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(panelIniciarSesionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(boton2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(boton3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout panelPrincipalLayout = new javax.swing.GroupLayout(panelPrincipal);
        panelPrincipal.setLayout(panelPrincipalLayout);
        panelPrincipalLayout.setHorizontalGroup(
            panelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPrincipalLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(panelIniciarSesion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelPrincipalLayout.setVerticalGroup(
            panelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPrincipalLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(panelIniciarSesion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout panelPedidosEnCursoLayout = new javax.swing.GroupLayout(panelPedidosEnCurso);
        panelPedidosEnCurso.setLayout(panelPedidosEnCursoLayout);
        panelPedidosEnCursoLayout.setHorizontalGroup(
            panelPedidosEnCursoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 352, Short.MAX_VALUE)
        );
        panelPedidosEnCursoLayout.setVerticalGroup(
            panelPedidosEnCursoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 75, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panelPrincipal, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(separador, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
                    .addComponent(panelPedidosEnCurso, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelPedidosEnCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(separador, javax.swing.GroupLayout.PREFERRED_SIZE, 5, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void digitoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_digitoActionPerformed
        if (contraseña.length() < 4) {
            JButton boton = (JButton) evt.getSource();
            contraseña += boton.getText();
            campoContraseña.setText(campoContraseña.getText() + "*");
        }
        if (contraseña.length() == 4) {
            botonBorrarContraseña.setText("OK");
        }    
    }//GEN-LAST:event_digitoActionPerformed

    private void botonBorrarContraseñaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBorrarContraseñaActionPerformed
        if (botonBorrarContraseña.getText().equals("OK")) {
            validarContraseña();
        }
        contraseña = "";
        campoContraseña.setText("");
        botonBorrarContraseña.setText("CE");
    }//GEN-LAST:event_botonBorrarContraseñaActionPerformed

    private void botonTituloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonTituloActionPerformed
        if (botonTitulo.isSelected()) {
            botonTitulo.setText("INICIO DE SESION");
        } else {
            botonTitulo.setText("CIERRE DE APLICACION");
        }
        campoContraseña.setText("");
        contraseña = "";
    }//GEN-LAST:event_botonTituloActionPerformed

    private void validarContraseña() {
        Empleado empleado = null;
        Conexion.abrirConexion();
        empleado = (Empleado) Conexion.enviarPeticion(new Peticion(new Empleado("Iniciar Sesion", contraseña, contraseña), Peticion.EMPLEADO));
        Conexion.cerrarConexion();

        if (empleado != null) {

            // CERRAR sesion
            if (!botonTitulo.isSelected()) {
                int cerrar = JOptionPane.showConfirmDialog(this, "Realmente desea cerrar la aplicacion?");
                if (cerrar == 0) {
                    Log.setEvento("Cliente", "INFO", empleado.getNombre() + " cierra la aplicacion");
                    System.exit(0);
                }
                return;
            }

            // Iniciar Sesion como MESERO
            if (empleado.getCargo().equals("Mesero")) {
                for (int i = 0; i < meseros.size(); i++) {
                    if (meseros.get(i).getEmpleado().equals(empleado)) {
                        meseros.get(i).setVisible(true);
                        Log.setEvento("Cliente", "INFO", "Se inicia la interfaz de Mesero.");
                        return;
                    }
                }
                meseros.add(new VistaMesero(this, empleado));
                Log.setEvento("Cliente", "INFO", "Se inicia la interfaz de Mesero.");
                return;
            }

            // Iniciar Sesion como COCINA
            if (empleado.getCargo().equals("Cocina")) {
                if (cocina == null) {
                    cocina = new VistaCocina(this, empleado);
                } else {
                    cocina.setVisible(true);
                }
                Log.setEvento("Cliente", "INFO", "Se incia la interfaz de Cocina.");
                return;
            }
        }
        // ERROR por contraseña incorrecta
        JOptionPane.showMessageDialog(this, "Contraseña incorrecta");
    }

    public static IniciarSesion getInstance() {
        if (instancia == null) {
            instancia = new IniciarSesion();
        }
        return instancia;
    }

    public void nuevoPedido(int mesa) {
        int espacio = 10;
        int tamaño = 50;
        pack();
        JToggleButton boton = new JToggleButton();
        boton.setFocusPainted(false);
        boton.setFont(new java.awt.Font("Tahoma", 0, 16));
        boton.setText(String.valueOf(mesa));
        boton.setSize(tamaño, tamaño);
        if (panelPedidosEnCurso.getComponentCount() == 0) {
            boton.setLocation(espacio, espacio);
        } else {
            boton.setLocation(panelPedidosEnCurso.getComponent(panelPedidosEnCurso.getComponentCount() - 1).getX() + tamaño + espacio, espacio);
        }
        panelPedidosEnCurso.add(boton);
        panelPedidosEnCurso.updateUI();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(new com.nilo.plaf.nimrod.NimRODLookAndFeel());
        } catch (UnsupportedLookAndFeelException ex) {
            Log.setEvento("Cliente", "WARNING", ex.getMessage());
        }
        Log.setEvento("Cliente", "INFO", "Se Ejecuta Interfaz de Iniciar Sesion.");
        IniciarSesion.getInstance().setVisible(true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton boton1;
    private javax.swing.JButton boton2;
    private javax.swing.JButton boton3;
    private javax.swing.JButton boton4;
    private javax.swing.JButton boton5;
    private javax.swing.JButton boton6;
    private javax.swing.JButton boton7;
    private javax.swing.JButton boton8;
    private javax.swing.JButton boton9;
    private javax.swing.JButton botonBorrarContraseña;
    private javax.swing.JToggleButton botonTitulo;
    private javax.swing.JTextField campoContraseña;
    private javax.swing.JLabel labelMensaje;
    private javax.swing.JPanel panelIniciarSesion;
    private javax.swing.JPanel panelPedidosEnCurso;
    private javax.swing.JPanel panelPrincipal;
    private javax.swing.JSeparator separador;
    // End of variables declaration//GEN-END:variables
}
